﻿using Microsoft.AspNetCore.WebUtilities;

namespace Micro.Common.Helper
{
    public sealed partial class UrlHelper
    {
        /// <summary>
        /// 获取url地址的host，port，path部分
        /// </summary>
        /// <param name="fullUrl"></param>
        /// <param name="host"></param>
        /// <param name="port"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Uri GetUrlParts(string fullUrl)
        {
            if (string.IsNullOrEmpty(fullUrl)) return default;
            Uri.TryCreate(fullUrl, UriKind.RelativeOrAbsolute, out Uri tempUri);
            return tempUri;
        }

        /// <summary>
        /// 获取Url后缀参数字典
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetParams(string url)
        {
            var dicParams = QueryHelpers.ParseQuery(url);
            return dicParams.ToDictionary(key => key.Key, value => value.Value.FirstOrDefault());
        }
    }
}